//
//  HYLPieView.m
//  饼图实现
//
//  Created by hyl on 16/6/24.
//  Copyright © 2016年 hyl. All rights reserved.
//

#import "HYLPieView.h"

@interface HYLPieView ()
@property (nonatomic,strong) NSMutableArray *ArrM;
@end
@implementation HYLPieView
#pragma mark -drawRect
- (void)drawRect:(CGRect)rect {
    [self setArrM];
    [self drawArc:rect];
}

#pragma mark - setArrM
-(void) setArrM{
    int total=100;
    int temp=0;
    self.ArrM=[NSMutableArray array];
    for (int i=0; i<=3; i++) {
        temp=arc4random_uniform(total);
        [self.ArrM addObject:@(temp)];

        total-=temp;
    }
    
    if (total) {
        [self.ArrM addObject:@(total)];
    }
    
}
#pragma mark - drawArc
-(void) drawArc:(CGRect) rect{
    
    for (int i=0; i<self.ArrM.count-1; i++) {
        
        CGFloat startA=0;
        
        CGFloat endA=startA+[self.ArrM[i] intValue];
        UIBezierPath *path=[UIBezierPath bezierPathWithArcCenter:CGPointMake(rect.size.width*0.5, rect.size.height*0.5) radius:rect.size.height*0.5 startAngle:startA endAngle:endA clockwise:YES];
        [[self colorRandom]set];
        [path addLineToPoint:CGPointMake(rect.size.width*0.5, rect.size.height*0.5)];
        [path fill];}

}

#pragma mark -colorRandom
-(UIColor *)colorRandom
{
    CGFloat r=arc4random_uniform(256)/255.0;
    CGFloat g=arc4random_uniform(256)/255.0;
    CGFloat b=arc4random_uniform(256)/255.0;
    return [UIColor colorWithRed:r green:g blue:b alpha:1];
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self setNeedsDisplay];
}
@end
